home *** CD-ROM | disk | FTP | other *** search
/ Aminet 24 / Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso / Aminet / dev / c / AmiVoGL_MDEV.lha / hershey / src / htext.c < prev    next >
C/C++ Source or Header  |  1997-12-26  |  14KB  |  748 lines

  1. /*
  2.  * This file is directly from the VOGLE library. It's simply had a few
  3.  * gratuitous name changes and some comments added.
  4.  */
  5. #ifdef SGI
  6. #include <gl.h>
  7. #include <device.h>
  8. #else
  9. #include "vogl.h"
  10. #include "vodevice.h"
  11. #endif
  12.  
  13. #include <stdio.h>
  14. // #include <fcntl.h>
  15. #include <stdlib.h>
  16. #include "hershey.h"
  17.  
  18. #ifdef TC
  19. extern double sin();
  20. extern double cos();
  21. #else
  22. #include <math.h>
  23. #endif
  24. #include <string.h>
  25.  
  26. #ifndef PATH_SIZE
  27. #define    PATH_SIZE    256
  28. #endif
  29.  
  30. #ifndef FONTLIB
  31. # ifdef PC
  32. #  define    FONTLIB        "c:\\lib\\hershey\\"
  33. # else
  34. #  ifdef AMIGA
  35. #   define    FONTLIB        "hershey:"
  36. #  else
  37. #   define    FONTLIB        "/usr/local/lib/hershey/"
  38. #  endif
  39. # endif
  40. #endif
  41.  
  42. #define    ABS(a)        (((a) < 0)? -(a) : (a))
  43. #define    MAX(a, b)    (((a) < (b))? (b) : (a))
  44. #define    XCOORD(x)    ((int)((x) - 'R'))
  45. #define    YCOORD(y)    ((int)('R' - (y)))
  46.  
  47. #ifndef PI
  48. #define PI     3.14159265358979
  49. #endif
  50.  
  51. #ifndef D2R
  52. #define D2R    (PI / 180.0)
  53. #endif
  54.  
  55. #define LEFT        0    /* The default */
  56. #define CENTERED    1
  57. #define RIGHT        2
  58.  
  59. static    float    tcos = 1.0, tsin = 0.0;            /* For rotations */
  60. static    float    SCSIZEX = 1.0, SCSIZEY = 1.0;        /* Scale factors */
  61. static    int    Justify = LEFT;
  62. static    int    Fixedwidth = 0;            /* Some flags     */
  63. static    short    nchars;                    /* No. in font     */
  64. int    hLoaded = 0;
  65.  
  66. #ifdef AZTEC_C
  67. #define __PROTOTYPE__
  68. #endif
  69.  
  70. #ifdef __PROTOTYPE__
  71. static int hershfont(char *);                          /* htext.c         */
  72. static int istrlength();                               /* htext.c         */
  73. #else
  74. static    hershfont();
  75. static int istrlength();
  76. #endif
  77.  
  78. static    char    old_font[PATH_SIZE] = "";    /* So we don't have to reload it */
  79. static    char    fpath[PATH_SIZE] = "";    
  80.  
  81. static    struct{
  82.     int   as;    /* Max ascender of a character in this font */
  83.     int   dec;    /* Max decender of a character in this font */
  84.     int   mw;    /* Max width of a character in this font */
  85.     char *p;    /* All the vectors in the font */
  86.     char **ind;    /* Pointers to where the chars start in p */
  87.     } ftab;
  88.  
  89. /* ---------------------------------------------------------------------
  90.  * Prototypes:
  91.  */
  92. #ifdef __PROTOTYPE__
  93.  
  94. static int hershfont(char *);                          /* htext.c         */
  95. static int istrlength(char *);                         /* htext.c         */
  96.  
  97. #else    /* __PROTOTYPE__ */
  98.  
  99. static int hershfont();                                /* htext.c         */
  100. static int istrlength();                               /* htext.c         */
  101.  
  102. #endif    /* __PROTOTYPE__ */
  103.  
  104. /* --------------------------------------------------------------------- */
  105.  
  106. /*
  107.  * hfont
  108.  *     loads in a hershey font.
  109.  */
  110. void hfont(char *name)
  111. {
  112.  
  113. /*
  114.  * check we aren't loading the same font twice in a row
  115.  */
  116. if (*name == '/') {
  117.     if (strcmp(strrchr(name, '/') + 1, old_font) == 0) {
  118.         return;
  119.         }
  120.     }
  121. else if (strcmp(name, old_font) == 0) {
  122.     return;
  123.     }
  124.  
  125. /*
  126.  * Try and load it
  127.  */
  128. if (!hershfont(name)) {
  129.     fprintf(stderr, "hershlib: problem reading font file '%s'.\n", name);
  130.     gexit();
  131.     exit(1);
  132.     }
  133.  
  134. /*
  135.  * Save the name of it...
  136.  */
  137. if (*name == '/') strcpy(old_font, strrchr(name, '/') + 1);
  138. else              strcpy(old_font, name);
  139.  
  140. }
  141.  
  142. /* ------------------------------------------------------------------------ */
  143.  
  144. /*
  145.  * hnumchars
  146.  *
  147.  *    Return the number of characters in the currently loaded hershey font.
  148.  */
  149. int hnumchars(void)
  150. {
  151. check_loaded("hnumchars");
  152.  
  153. return (int) nchars;
  154. }
  155.  
  156. /* ------------------------------------------------------------------------ */
  157.  
  158. /*
  159.  * hsetpath
  160.  *
  161.  *    Set the path of the directory to look for fonts.
  162.  */
  163. void hsetpath(char *path)
  164. {
  165. int    l;
  166.  
  167. strcpy(fpath, path);
  168.  
  169. l = strlen(fpath);
  170.  
  171. #ifdef PC
  172. if (fpath[l] != '\\') strcat(fpath, "\\");
  173. #else
  174. # ifdef AMIGA
  175. if (fpath[l] != '/' && fpath[l] != ':') strcat(fpath, "/");
  176. # else
  177. if (fpath[l] != '/') strcat(fpath, "/");
  178. # endif
  179. #endif
  180. }
  181.  
  182. /* ------------------------------------------------------------------------ */
  183.  
  184. /*
  185.  * hershfont
  186.  *
  187.  * Load in a hershey font. First try the environment, then the font library,
  188.  * if that fails try the current directory, otherwise return 0.
  189.  */
  190. static int hershfont(char *fontname)
  191. {
  192. FILE   *fp;
  193. int     i;
  194. // int     j;
  195. short   nvects;
  196. short   n;
  197. char   *flib;
  198. char    path[PATH_SIZE];    
  199.  
  200.  
  201. if (fpath[0] != '\0') {
  202.     strcpy(path, fpath);
  203.     strcat(path, fontname);
  204.     }
  205. else if ((flib = getenv("HFONTLIB")) != (char *)NULL) {
  206.     strcpy(path, flib);
  207. #ifdef PC
  208.     strcat(path, "\\");
  209. #else
  210. # ifdef AMIGA
  211.     if(path[strlen(path) - 1] != ':') strcat(path,"/");
  212. # else
  213.     strcat(path, "/");
  214. #endif
  215. #endif
  216.     strcat(path, fontname);
  217.     }
  218. else if ((flib = getenv("VFONTLIB")) != (char *)NULL) {
  219.     strcpy(path, flib);    /* To be compatible with VOGLE */
  220. #ifdef PC
  221.     strcat(path, "\\");
  222. #else
  223. # ifdef AZTEC_C
  224.     if(path[strlen(path) - 1] != ':') strcat(path,"/");
  225. # else
  226.     strcat(path, "/");
  227. # endif
  228. #endif
  229.     strcat(path, fontname);
  230.     }
  231. else {
  232.     strcpy(path, FONTLIB);
  233. #ifdef PC
  234.     strcat(path, "\\");
  235. #else
  236. # ifdef AMIGA
  237.     if(path[strlen(path) - 1] != ':') strcat(path,"/");
  238. # else
  239.     strcat(path, "/");
  240. # endif
  241. #endif
  242.     strcat(path, fontname);
  243.     }
  244.  
  245.  
  246. #ifdef PC
  247. if ((fp = fopen(path, "r+b")) == (FILE *)NULL &&
  248.     (fp = fopen(fontname, "r+b")) == (FILE *)NULL)
  249. #else
  250. # ifdef AMIGA
  251. if ((fp= fopen(path, "rb"))     == (FILE *) NULL &&
  252.     (fp= fopen(fontname, "rb")) == (FILE *) NULL)
  253. # else
  254. if ((fp= fopen(path, "r"))     == (FILE *) NULL &&
  255.     (fp= fopen(fontname, "r")) == (FILE *) NULL)
  256. # endif
  257. #endif
  258.     {
  259.     fprintf(stderr, "hershlib: Can't open Hershey fontfile '%s' or './%s'.\n",
  260.       path,
  261.       fontname);
  262.     exit(1);
  263.     }
  264.  
  265. if (fread(&nchars, sizeof(nchars), 1, fp) != 1) {
  266.     return 0;
  267.     }
  268.  
  269.  
  270. if (fread(&nvects, sizeof(nvects), 1, fp) != 1) {
  271.     return 0;
  272.     }
  273.  
  274. if (fread(&n, sizeof(n), 1,  fp) != 1) {
  275.     return 0;
  276.     }
  277.  
  278. ftab.as = (int)n;
  279.  
  280. if (fread(&n, sizeof(n), 1, fp) != 1) {
  281.     return 0;
  282.     }
  283.  
  284. ftab.dec = (int)n;
  285.  
  286. if (fread(&n, sizeof(n), 1, fp) != 1) {
  287.     return 0;
  288.     }
  289.  
  290.  
  291. ftab.mw = (int)n;
  292.  
  293. /*
  294.  *  Allocate space for it all....
  295.  */
  296. if (hLoaded) {
  297.     if (ftab.ind[0]) free(ftab.ind[0]);
  298.     if (ftab.ind)    free(ftab.ind);
  299.     hLoaded = 0;
  300.     }
  301.  
  302. ftab.ind = (char **)hallocate(sizeof(char *)*(nchars + 1));
  303. ftab.p   = (char *)hallocate((unsigned)(2 * nvects));
  304.  
  305. /*
  306.  *  As we read in each character, figure out what ind should be
  307.  */
  308.  
  309. for (i = 0; i < nchars; i++) {
  310.     if (fread(&n , sizeof(n), 1, fp) != 1) {
  311.         return 0;
  312.         }
  313.  
  314.     if (fread(ftab.p, 1, (unsigned)n, fp) != (unsigned)n) {
  315.         return 0;
  316.         }
  317.  
  318.     ftab.ind[i] = ftab.p;
  319.     ftab.p     += n;
  320.     }
  321.  
  322. ftab.ind[nchars] = ftab.p;    /* To Terminate the last one */
  323.  
  324.  
  325. fclose(fp);
  326. hLoaded = 1;
  327.  
  328. return 1;
  329. }
  330.  
  331. /* ------------------------------------------------------------------------ */
  332.  
  333. /*
  334.  * hgetcharsize
  335.  *
  336.  *    get the width and height of a single character. At the moment, for
  337.  * the hershey characters, the height returned is always that of the
  338.  * difference between the maximun descender and ascender.
  339.  *
  340.  */
  341. void hgetcharsize(char c, float *width, float *height)
  342. {
  343. check_loaded("hgetcharsize");
  344.  
  345. *height = (float)(ftab.as - ftab.dec) * SCSIZEY;
  346.  
  347. if (Fixedwidth)
  348. *width = (float)ftab.mw * SCSIZEX;
  349. else
  350. *width = (float)(ftab.ind[c - 32][1] - ftab.ind[c - 32][0]) * SCSIZEX;
  351. }
  352.  
  353. /* ------------------------------------------------------------------------ */
  354.  
  355. /*
  356.  * hdrawchar
  357.  *
  358.  * Display a character from the currently loaded font.
  359.  */
  360. void hdrawchar(int c)
  361. {
  362. char    *p, *e;
  363. int    Move, i, x, y, xt, yt;
  364. float    xs, ys, xp, yp, tmp, xtmp, ytmp;
  365.  
  366.  
  367. check_loaded("hdrawchar");
  368.  
  369. if ((i = c - 32) < 0)
  370. i = 0;
  371. if (i >= nchars)
  372. i = nchars - 1;
  373.  
  374. Move = 1;
  375.  
  376. xt = yt = 0;
  377. if (Justify == LEFT) {
  378.     xt = (Fixedwidth ? -ftab.mw / 2 : XCOORD(ftab.ind[i][0]));
  379.     yt = ftab.dec;
  380.     }
  381. else if (Justify == RIGHT) {
  382.     xt = (Fixedwidth ?  ftab.mw / 2 : -XCOORD(ftab.ind[i][0]));
  383.     yt = ftab.dec;
  384.     }
  385.  
  386. e = ftab.ind[i + 1];
  387. p = ftab.ind[i] + 2;
  388.  
  389. xtmp = ytmp = 0.0;
  390.  
  391. while(p < e) {
  392.     x = XCOORD((int)*p++);
  393.     y = YCOORD((int)*p++);
  394.     if (x != -50) {            /* means move */
  395.         xp = (float)(x - xt) * SCSIZEX;
  396.         yp = (float)(y - yt) * SCSIZEY;
  397.         tmp = xp;
  398.         xp = tcos*tmp - tsin*yp;
  399.         yp = tsin*tmp + tcos*yp;
  400.         xs = xp - xtmp;
  401.         ys = yp - ytmp;
  402.         xtmp = xp;
  403.         ytmp = yp;
  404.         if (Move) {
  405.             Move = 0;
  406.             rmv((Coord)xs, (Coord)ys, (Coord)0.0);
  407.             }
  408.         else
  409.         rdr((Coord)xs, (Coord)ys, (Coord)0.0);
  410.  
  411.         }
  412.     else {
  413.         Move = 1;
  414.         }
  415.     }
  416. /*
  417.  * Move to right hand of character.
  418.    BLARK
  419.  */
  420.  
  421. tmp = Fixedwidth ? (float)ftab.mw : (float)(ftab.in